package com.yf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.dto.Page;
import com.yf.entity.Class;
import com.yf.mapper.ClassMapper;
import com.yf.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Override
    public void addClass(Class cls) {
        classMapper.addClass(cls);
    }

    @Override
    public void deleteClass(Integer classId) {
        classMapper.deleteClass(classId);
    }

    @Override
    public void updateClass(Class cls) {
        classMapper.updateClass(cls);
    }

    @Override
    public Class getClassById(Integer classId) {
        return classMapper.getClassById(classId);
    }

    @Override
    public Page getClassPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();

        PageHelper.startPage(pageNo,pageSize);
        List<Class> classList = classMapper.getClassList();
        PageInfo<Class> info = new PageInfo<>(classList);

        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setPageCount(info.getPages());
        page.setCount((int) info.getTotal());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setData(info.getList());

        return page;
    }
}
